package com.wanxi.education.controller.back;

import com.github.pagehelper.PageInfo;
import com.wanxi.education.common.CommonResult;
import com.wanxi.education.model.vo.advertandcertificate.AdvertAddVo;
import com.wanxi.education.model.vo.advertandcertificate.CertificateAddVo;
import com.wanxi.education.model.vo.advertandcertificate.CertificateFindPageVo;
import com.wanxi.education.service.CertificateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: yanguangqiong
 * @Description:
 * @Date: Created in 2021/9/24
 * @Version: 1.0
 */
@Api(tags = "后台管理员证书管理")
@RestController
@RequestMapping("/admin/back/certificate")
public class CertificateBackController {
    @Autowired
    private CertificateService certificateService;
    @PostMapping("/findPage")
    @ApiOperation("后台管理员查看证书列表")
    public CommonResult<PageInfo<CertificateAddVo>> findPage(@RequestBody CertificateFindPageVo certificateFindPageVo) {
        if (ObjectUtils.isEmpty(certificateFindPageVo.getPageNum())) {
            return CommonResult.failure("第几页为空");
        }
        if (ObjectUtils.isEmpty(certificateFindPageVo.getPageSize())) {
            return CommonResult.failure("每页几条为空");
        }
        PageInfo page = certificateService.findPage(certificateFindPageVo);
        return CommonResult.success(page);
    }
    @GetMapping("/findById")
    @ApiOperation("后台管理员查看证书详情")
    public CommonResult<CertificateAddVo> findById(@RequestParam("certificateId") Long certificateId) {
        if (ObjectUtils.isEmpty(certificateId)) {
            return CommonResult.failure("证书Id为空");
        }
        CertificateAddVo certificateAddVo =certificateService.findById(certificateId);
        return CommonResult.success(certificateAddVo);
    }
    /*
    *
    * */
    @PostMapping("/add")
    @ApiOperation("后台管理员增加证书信息")
    public CommonResult add(@RequestBody CertificateAddVo certificateAddVo){
        if (StringUtils.isBlank(certificateAddVo.getName())){
            return CommonResult.failure("证书名称为空");
        }if (StringUtils.isBlank(certificateAddVo.getInstitutional())){
            return CommonResult.failure("组织机构名称为空");
        }if (StringUtils.isBlank(certificateAddVo.getPurpose())){
            return CommonResult.failure("证书用途为空");
        }if (ObjectUtils.isEmpty(certificateAddVo.getClassificationId())){
            return CommonResult.failure("证书类型为空");
        }if (ObjectUtils.isEmpty(certificateAddVo.getPicture())){
            return CommonResult.failure("证书图片为空");
        }
        certificateService.add(certificateAddVo);
        return CommonResult.success("success");
    }
    /*
    *
    * */
    @PostMapping("/update")
    @ApiOperation("后台管理员修改证书信息")
    public CommonResult update(@RequestBody CertificateAddVo certificateAddVo){

        if (StringUtils.isBlank(certificateAddVo.getName())){
            return CommonResult.failure("证书名称为空");
        }
        if (ObjectUtils.isEmpty(certificateAddVo.getId())){
            return CommonResult.failure("证书id为空");
        }
        if (ObjectUtils.isEmpty(certificateAddVo.getClassificationId())){
            return CommonResult.failure("证书类型为空");
        }
        if (StringUtils.isBlank(certificateAddVo.getInstitutional())){
            return CommonResult.failure("组织机构名称为空");
        }if (ObjectUtils.isEmpty(certificateAddVo.getPicture())){
            return CommonResult.failure("证书图片为空");
        }
        if (StringUtils.isBlank(certificateAddVo.getPurpose())){
            return CommonResult.failure("证书用途为空");
        }
        certificateService.update(certificateAddVo);
        return CommonResult.success("success");
    }
    @PostMapping("/updateStatus")
    @ApiOperation("后台管理员修改证书状态")
    public CommonResult updateStatus(@RequestParam("certificateId") Long certificateId){
        if (ObjectUtils.isEmpty(certificateId)) {
            return CommonResult.failure("证书Id为空");
        }
        certificateService.updateStatus(certificateId);
        return CommonResult.success("success");
    }
    @PostMapping("/delete")
    @ApiOperation("后台管理员删除证书信息")
    public CommonResult delete(@RequestParam("certificateId") Long certificateId){
        if (ObjectUtils.isEmpty(certificateId)) {
            return CommonResult.failure("证书Id为空");
        }
        certificateService.delete(certificateId);
        return CommonResult.success("success");
    }
}
